﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using CakeAnimation;

namespace CharacterAnimationEditor
{
    public class OnionGUI
    {
        Skeleton m_Skeleton;

        float m_CurrTime;
        float m_Speed;

        bool m_ViewKeyframes = true;

        SkeletonView m_View;

        public OnionGUI(Context context)
        {
            m_CurrTime = 0.0f;
            m_Skeleton = new Skeleton(context.AnimationContext, "SkeletonRig.txt");
            m_View = new SkeletonView();
            m_View.MySkeleton = m_Skeleton;
            m_View.Offset = new Vector2(100, 100);
            m_View.Scale = 0.6f;

            m_Speed = 1.0f;
        }

        public void Update(Context context)
        {
            if (context.PrevKeyState.IsKeyUp(Keys.O) && context.CurrKeyState.IsKeyDown(Keys.O))
            {
                m_ViewKeyframes = !m_ViewKeyframes;
            }

            m_View.Offset = new Vector2(100, 100) + context.CameraPos;
            m_View.Scale = context.Zoom;
        }

        public void Draw(Context context)
        {
            KeyFrameController controller = new KeyFrameController();

            m_CurrTime = context.CurrentKeyFrame.Time;
            for (int i = -3; i < 4; ++i)
            {
                if (i == 0)
                    continue;

                float alpha = 0.75f - Math.Abs(i * 0.2f);

                if (m_ViewKeyframes)
                {
                    int kfIndex = context.Animation.GetKeyFrameBeforeTime( m_CurrTime + 0.001f);
                    kfIndex += i;

                    if (0 <= kfIndex && kfIndex < context.Animation.m_Keyframes.Count)
                    {
                        KeyFrame kf = context.Animation.m_Keyframes[kfIndex];


                        //controller.InterplateSkeleton(skeleton, kf.Pose, kf.Pose, 0.0f);
                        //skeleton.Body.UpdateTransform();
                        context.Animation.GetPoseAt(kf.Time, m_Skeleton);
                        //view.MySkeleton = skeleton;
                        m_View.Alpha = alpha;
                        m_View.Tint = i < 0 ? Color.Red : Color.Green;
                        m_View.ZOffset = 1.0f;
                        m_View.Draw(context);
                    }
                }
                else
                {
                    float realTime = m_CurrTime + i * 0.25f;

                    int kfIndex = context.Animation.GetKeyFrameBeforeTime(realTime);
                    if (0 <= kfIndex && kfIndex < context.Animation.m_Keyframes.Count - 1)
                    {
                        KeyFrame kf1 = context.Animation.m_Keyframes[kfIndex];
                        KeyFrame kf2 = context.Animation.m_Keyframes[kfIndex + 1];

                        float t = (realTime - kf1.Time) / (kf2.Time - kf1.Time);
                        controller.InterplateSkeleton(m_Skeleton, kf1.Pose, kf2.Pose, t);
                        m_Skeleton.RootBone.UpdateTransform();
                        m_View.Alpha = alpha;
                        m_View.Tint = i < 0 ? Color.Red : Color.Green;
                        m_View.ZOffset = 0.3f;
                        m_View.Draw(context);
                    }

                }

            }
        }
    }
}
